package Application.Summoner;

import Application.Config.Config;
import Application.MethodObject.DefGuns;
import Application.Others.FunnyJoke;
import Application.Utils.FileOutputUtil;
import Application.Utils.StaticUtil;
import Application.Widgets.FormattedTextFileds.TextFieldInteger;
import Application.Widgets.Other.ListPanelForTags;
import Application.Widgets.Override.ButtonOverride;
import Application.Widgets.Override.CheckBoxOverride;
import Application.Widgets.Override.LabelOverride;
import Application.Widgets.Override.PanelOverride;
import Application.Widgets.TextFileds.TextFiledDouble;
import Application.Widgets.TextFileds.TextFiledString;
import Application.Widgets.TextList.TextListString;
import Application.Writer.XamlWriter;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.ArrayList;

import static Application.Utils.StaticUtil.chooser;

public class GunsSummoner
{
    public static void GunSummoner()
    {
        DefGuns def = new DefGuns();
        StaticUtil.Init_config();
        Config config = StaticUtil.config;
        FunnyJoke funnyJoke = new FunnyJoke();

        StaticUtil.summonerFrame = new JFrame();
        JFrame mainFrame = StaticUtil.summonerFrame;
        mainFrame.setTitle("边缘世界ModXAML生成器");
        mainFrame.setIconImage(new ImageIcon("src/Asset/Rim-Craft.png").getImage());
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);

        JPanel errPanel = StaticUtil.errorPanel;
        StaticUtil.Init_errorReportPane();
        JButton funnyJokes = new JButton("看看笑话");
        funnyJokes.setFont(config.fontGetter());
        funnyJokes.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                StaticUtil.errorReport.append(funnyJoke.getJokes_1() + "\n" + "笑话来源于网络\n");
            }
        });

        JPanel mainPanel = new JPanel();
        BoxLayout Box_Y = new BoxLayout(mainPanel, BoxLayout.Y_AXIS);
        mainPanel.setLayout(Box_Y);
        StaticUtil.Init_menuBar(mainFrame);

        JScrollPane scrollPane_2 = new JScrollPane(mainPanel);
        scrollPane_2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane_2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

        //LabelOverride BGLabel = new LabelOverride(new ImageIcon("src/Asset/BG.png"),mainFrame,mainPanel);

        JLabel lab_XAMLName = new JLabel("请键入文件名称");
        lab_XAMLName.setFont(config.fontGetter());
        TextFiledString tf_XAMLName = new TextFiledString(def.XAMLName);
        tf_XAMLName.setFont(config.fontGetter());
        tf_XAMLName.setWidgetName("文件名称设置器");
        mainPanel.add(lab_XAMLName);
        mainPanel.add(tf_XAMLName);

        JLabel lab_bullet_ParentName = new JLabel("请输入子弹父类名称");
        lab_bullet_ParentName.setFont(config.fontGetter());
        TextFiledString tf_bullet_ParentName = new TextFiledString(def.bullet_ParentName);
        tf_bullet_ParentName.setFont(config.fontGetter());
        tf_bullet_ParentName.setWidgetName("子弹父类设置器");
        mainPanel.add(lab_bullet_ParentName);
        mainPanel.add(tf_bullet_ParentName);

        JLabel lab_bullet_defName = new JLabel("请输入子弹注册名");
        lab_bullet_defName.setFont(config.fontGetter());
        TextFiledString tf_bullet_defName = new TextFiledString(def.bullet_defName);
        tf_bullet_defName.setFont(config.fontGetter());
        tf_bullet_defName.setWidgetName("子弹注册名设置器");
        mainPanel.add(lab_bullet_defName);
        mainPanel.add(tf_bullet_defName);

        JLabel lab_bullet_label = new JLabel("请输入子弹显示名");
        lab_bullet_label.setFont(config.fontGetter());
        TextFiledString tf_bullet_label = new TextFiledString(def.bullet_label);
        tf_bullet_label.setFont(config.fontGetter());
        tf_bullet_label.setWidgetName("子弹显示名设置器");
        mainPanel.add(lab_bullet_label);
        mainPanel.add(tf_bullet_label);

        JLabel lab_bullet_texPath = new JLabel("请输入子弹材质路径");
        lab_bullet_texPath.setFont(config.fontGetter());
        TextFiledString tf_bullet_texPath = new TextFiledString(def.bullet_texPath);
        tf_bullet_texPath.setFont(config.fontGetter());
        tf_bullet_texPath.setWidgetName("子弹材质设置器");
        mainPanel.add(lab_bullet_texPath);
        mainPanel.add(tf_bullet_texPath);

        JLabel lab_bullet_damageDef = new JLabel("请输入子弹伤害类型");
        lab_bullet_damageDef.setFont(config.fontGetter());
        TextFiledString tf_bullet_damageDef = new TextFiledString(def.bullet_damageDef);
        tf_bullet_damageDef.setFont(config.fontGetter());
        tf_bullet_damageDef.setWidgetName("子弹伤害类型设置器");
        mainPanel.add(lab_bullet_damageDef);
        mainPanel.add(tf_bullet_damageDef);

        JLabel lab_bullet_damageAmountBase = new JLabel("请输入子弹基本伤害");
        lab_bullet_damageAmountBase.setFont(config.fontGetter());

        TextFieldInteger tf_bullet_damageAmountBase = new TextFieldInteger(20);
        tf_bullet_damageAmountBase.setFont(config.fontGetter());
        tf_bullet_damageAmountBase.setWidgetName("子弹基本伤害设置器");

        JLabel lab_bullet_stoppingPower = new JLabel("请输入子弹制动力");
        lab_bullet_stoppingPower.setFont(config.fontGetter());

        TextFiledDouble tf_bullet_stoppingPower = new TextFiledDouble(def.bullet_stoppingPower);
        tf_bullet_stoppingPower.setFont(config.fontGetter());
        tf_bullet_stoppingPower.setWidgetName("子弹制动力设置器");

        JLabel lab_bullet_speed = new JLabel("请输入子弹飞行速度");
        lab_bullet_speed.setFont(config.fontGetter());

        TextFieldInteger tf_bullet_speed = new TextFieldInteger(def.bullet_speed);
        tf_bullet_speed.setFont(config.fontGetter());
        tf_bullet_speed.setWidgetName("子弹飞行速度设置器");

        JLabel lab_gun_ParentName = new JLabel("请输入枪械父类");
        lab_gun_ParentName.setFont(config.fontGetter());

        TextFiledString tf_gun_ParentName = new TextFiledString(def.gun_ParentName);
        tf_gun_ParentName.setFont(config.fontGetter());
        tf_gun_ParentName.setWidgetName("枪械父类设置器");

        JLabel lab_gun_defName = new JLabel("请输入枪械注册名");
        lab_gun_defName.setFont(config.fontGetter());

        TextFiledString tf_gun_defName = new TextFiledString(def.gun_defName);
        tf_gun_defName.setFont(config.fontGetter());
        tf_gun_defName.setWidgetName("枪械注册名设置器");

        JLabel lab_gun_label = new JLabel("请输入枪械显示名");
        lab_gun_label.setFont(config.fontGetter());

        TextFiledString tf_gun_label = new TextFiledString(def.gun_label);
        tf_gun_label.setFont(config.fontGetter());
        tf_gun_label.setWidgetName("枪械显示名设置器");

        JLabel lab_gun_description = new JLabel("请设置枪械描述(使用\\n字符进行换行)");
        lab_gun_description.setFont(config.fontGetter());

        TextFiledString tf_gun_description = new TextFiledString(def.gun_description);
        tf_gun_description.setFont(config.fontGetter());
        tf_gun_description.setWidgetName("枪械描述设置器");

        mainPanel.add(lab_bullet_stoppingPower);
        mainPanel.add(tf_bullet_stoppingPower);
        mainPanel.add(lab_bullet_speed);
        mainPanel.add(tf_bullet_speed);
        mainPanel.add(lab_gun_ParentName);
        mainPanel.add(tf_gun_ParentName);
        mainPanel.add(lab_gun_defName);
        mainPanel.add(tf_gun_defName);
        mainPanel.add(lab_gun_label);
        mainPanel.add(tf_gun_label);
        mainPanel.add(lab_gun_description);
        mainPanel.add(tf_gun_description);

        TextFiledString tf_gun_texPath = new TextFiledString("请输入枪械材质路径",def.gun_texPath,"枪械材质设置器",mainPanel);
        LabelOverride lab_gun_uiIconScale = new LabelOverride("请输入枪械显示缩放大小（标准值:1.4）",mainPanel);
        TextFiledDouble tf_gun_uiIconScale = new TextFiledDouble(def.gun_uiIconScale,"枪械缩放设置器",mainPanel);
        LabelOverride lab_gun_thingSetMakerTags = new LabelOverride("请输入枪械制作标签（留空将使用默认值）",mainPanel);
        TextFiledString tf_gun_thingSetMakerTags = new TextFiledString(def.gun_thingSetMakerTags,"枪械制作标签设置器",mainPanel);
        LabelOverride lab_gun_WorkToMake = new LabelOverride("请输入枪械制作耗时",mainPanel);
        TextFieldInteger tf_gun_WorkToMake = new TextFieldInteger(def.gun_WorkToMake,"枪械制作耗时设置器",mainPanel);
        LabelOverride lab_gun_Mass = new LabelOverride("请输入枪械质量",mainPanel);
        TextFiledDouble tf_gun_Mass = new TextFiledDouble(def.gun_Mass,"枪械质量设置器",mainPanel);
        LabelOverride lab_gun_AccuracyShort = new LabelOverride("情输入枪械短距离命中率",mainPanel);
        TextFiledDouble tf_gun_AccuracyShort = new TextFiledDouble(def.gun_AccuracyShort,"枪械短距离命中率设置器",mainPanel);
        LabelOverride lab_gun_AccuracyMedium = new LabelOverride("请输入枪械中距离命中率",mainPanel);
        TextFiledDouble tf_gun_AccuracyMedium = new TextFiledDouble(def.gun_AccuracyMedium,"枪械中距离命中率设置器",mainPanel);
        LabelOverride lab_gun_AccuracyLong = new LabelOverride("请输入枪械远距离命中率",mainPanel);
        TextFiledDouble tf_gun_AccuracyLong = new TextFiledDouble(def.gun_AccuracyLong,"枪械远距离命中率设置器",mainPanel);
        LabelOverride lab_gun_RangedWeapon_Cooldown = new LabelOverride("请输入枪械射击冷却",mainPanel);
        TextFiledDouble tf_gun_RangedWeapon_Cooldown = new TextFiledDouble(def.gun_RangedWeapon_Cooldown,"枪械冷却设置器",mainPanel);

        LabelOverride lab_gun_weaponTags = new LabelOverride("请为你的枪械添加标签",mainPanel);
        PanelOverride tagsPanel = new PanelOverride(mainPanel);
        tagsPanel.setLayout(new BoxLayout(tagsPanel, BoxLayout.Y_AXIS));
        ArrayList<LabelOverride> lab_labList_gun_weaponTags = new ArrayList<>();
        ArrayList<TextFiledString> tf_StringList_gun_weaponTags = new ArrayList<>();
        ButtonOverride addButton_gun_weaponTags = new ButtonOverride("添加标签",mainPanel,e ->
        {
            lab_labList_gun_weaponTags.add(new LabelOverride("标签" + (lab_labList_gun_weaponTags.size()+1),tagsPanel));
            tf_StringList_gun_weaponTags.add(new TextFiledString("","枪械标签设置器" + (tf_StringList_gun_weaponTags.size()+1),tagsPanel));
            SwingUtilities.updateComponentTreeUI(tagsPanel);
        });
        ButtonOverride deleteButton_gun_weaponTags = new ButtonOverride("移除最后一个标签",mainPanel,e ->
        {
            if (lab_labList_gun_weaponTags.isEmpty() || tf_StringList_gun_weaponTags.isEmpty())
            {
                StaticUtil.errorReport.append("您目前没有标签可以去除\n");
            }else
            {
                tagsPanel.remove(lab_labList_gun_weaponTags.get(lab_labList_gun_weaponTags.size()-1));
                lab_labList_gun_weaponTags.remove(lab_labList_gun_weaponTags.size()-1);
                tagsPanel.remove(tf_StringList_gun_weaponTags.get(tf_StringList_gun_weaponTags.size()-1));
                tf_StringList_gun_weaponTags.remove(tf_StringList_gun_weaponTags.size()-1);
                SwingUtilities.updateComponentTreeUI(tagsPanel);
            }
        });
        LabelOverride lab_gun_costList = new LabelOverride("请为你的枪械添加合成材料列表",mainPanel);
        PanelOverride costListPanel = new PanelOverride(mainPanel);
        costListPanel.setLayout(new BoxLayout(costListPanel, BoxLayout.Y_AXIS));
        ArrayList<LabelOverride> lab_labList_gun_costList = new ArrayList<>();
        ArrayList<TextFiledString> tf_StringList_gun_costList = new ArrayList<>();
        ArrayList<LabelOverride> lab_labList_gun_costListNum = new ArrayList<>();
        ArrayList<TextFieldInteger> tf_IntegerList_gun_costListNum = new ArrayList<>();
        ButtonOverride addButton_gun_costList = new ButtonOverride("添加材料",mainPanel,e ->
        {
            lab_labList_gun_costList.add(new LabelOverride("材料" + (lab_labList_gun_costList.size()+1),costListPanel));
            tf_StringList_gun_costList.add(new TextFiledString("","枪械材料设置器" + (tf_StringList_gun_costList.size()+1),costListPanel));
            lab_labList_gun_costListNum.add(new LabelOverride("材料" + (lab_labList_gun_costListNum.size()+1)+"数量",costListPanel));
            tf_IntegerList_gun_costListNum.add(new TextFieldInteger(1,"枪械材料数量设置器" + (tf_IntegerList_gun_costListNum.size()+1),costListPanel));
            SwingUtilities.updateComponentTreeUI(costListPanel);
        });
        ButtonOverride deleteButton_gun_costList = new ButtonOverride("移除最后一个材料",mainPanel,e ->
        {
            if (lab_labList_gun_costList.isEmpty() || tf_StringList_gun_costList.isEmpty())
            {
                StaticUtil.errorReport.append("您目前没有材料可以去除\n");
            }else
            {
                costListPanel.remove(lab_labList_gun_costList.get(lab_labList_gun_costList.size()-1));
                lab_labList_gun_costList.remove(lab_labList_gun_costList.size()-1);
                costListPanel.remove(tf_StringList_gun_costList.get(tf_StringList_gun_costList.size()-1));
                tf_StringList_gun_costList.remove(tf_StringList_gun_costList.size()-1);
                costListPanel.remove(lab_labList_gun_costListNum.get(lab_labList_gun_costListNum.size()-1));
                lab_labList_gun_costListNum.remove(lab_labList_gun_costListNum.size()-1);
                costListPanel.remove(tf_IntegerList_gun_costListNum.get(tf_IntegerList_gun_costListNum.size()-1));
                tf_IntegerList_gun_costListNum.remove(tf_IntegerList_gun_costListNum.size()-1);
                SwingUtilities.updateComponentTreeUI(costListPanel);
            }
        });
        LabelOverride lab_gun_skillRequirements_Craft = new LabelOverride("请输入最低制作等级（制造）",mainPanel);
        TextFieldInteger tf_gun_skillRequirements_Craft = new TextFieldInteger(1,"枪械制作等级要求设置器",mainPanel);
        CheckBoxOverride cb_gun_hasStandardCommand = new CheckBoxOverride("是否允许手动操作",true,mainPanel);
        LabelOverride lab_gun_warmupTime = new LabelOverride("请输入枪械预热时间（前摇）",mainPanel);
        TextFiledDouble tf_gun_warmupTime = new TextFiledDouble(0.3,"枪械预热时间设置器",mainPanel);
        LabelOverride lab_gun_range = new LabelOverride("请输入枪械射程（整数）",mainPanel);
        TextFieldInteger tf_gun_range = new TextFieldInteger(1,"枪械射程设置器",mainPanel);
        LabelOverride lab_gun_soundCast = new LabelOverride("请设置枪械攻击音效（留空将使用Shot_Revolver——左轮开火音效）",mainPanel);
        TextFiledString tf_gun_soundCast = new TextFiledString(def.gun_soundCast,"枪械开火音效设置器",mainPanel);
        LabelOverride lab_gun_soundCastTail = new LabelOverride("请输入枪械命中音效（留空将使用轻武器命中音效）",mainPanel);
        TextFiledString tf_gun_soundCastTail = new TextFiledString(def.gun_soundCastTail,"枪械命中音效设置器",mainPanel);
        LabelOverride lab_gun_muzzleFlashScale = new LabelOverride("请输入枪械开火光圈缩放（左轮手枪默认值：9）",mainPanel);
        TextFieldInteger tf_gun_muzzleFlashScale = new TextFieldInteger(1,"枪械开火光圈缩放设置器",mainPanel);

        ButtonOverride setSavePath = new ButtonOverride("请选择默认打开路径",mainPanel,choiceEvent -> FileOutputUtil.SetSavePath(mainFrame));
        ButtonOverride summon = new ButtonOverride("生成XAML文件",mainPanel,event ->
        {
            def.XAMLName = tf_XAMLName.getValue();
            def.bullet_ParentName = tf_bullet_ParentName.getValue();
            def.bullet_defName = tf_bullet_defName.getValue();
            def.bullet_label = tf_bullet_label.getValue();
            def.bullet_texPath = tf_bullet_texPath.getValue();
            def.bullet_damageDef = tf_bullet_damageDef.getValue();
            def.bullet_damageAmountBase = tf_bullet_damageAmountBase.getInteger(def.bullet_damageAmountBase);
            def.bullet_stoppingPower = tf_bullet_stoppingPower.getValue(0);
            def.bullet_speed = tf_bullet_speed.getInteger(def.bullet_speed);

            def.gun_ParentName = tf_gun_ParentName.getValue();
            def.gun_defName = tf_gun_defName.getValue();
            def.gun_label = tf_gun_label.getValue();
            def.gun_description = tf_gun_description.getValue();
            def.gun_texPath = tf_gun_texPath.getValue();
            def.gun_uiIconScale = tf_gun_uiIconScale.getValue(0);
            def.gun_thingSetMakerTags = tf_gun_thingSetMakerTags.getValue();
            def.gun_WorkToMake = tf_gun_WorkToMake.getInteger(def.gun_WorkToMake);
            def.gun_Mass = tf_gun_Mass.getValue(0);
            def.gun_AccuracyTouch = tf_gun_AccuracyLong.getValue(0,1);
            def.gun_AccuracyShort = tf_gun_AccuracyShort.getValue(0,1);
            def.gun_AccuracyMedium = tf_gun_AccuracyMedium.getValue(0,1);
            def.gun_AccuracyLong = tf_gun_AccuracyLong.getValue(0,1);
            def.gun_RangedWeapon_Cooldown = tf_gun_RangedWeapon_Cooldown.getValue(0);
            ArrayList<String> tagsList = new ArrayList<>();
            tf_StringList_gun_weaponTags.forEach(addEventTags -> tagsList.add(addEventTags.getValue()));
            def.gun_weaponTags = StaticUtil.StringListGetter("li",tagsList.toArray());
            ArrayList<String> costList = new ArrayList<>();
            ArrayList<Integer> costListNum = new ArrayList<>();
            tf_StringList_gun_costList.forEach(addEventCosts -> costList.add(addEventCosts.getValue()));
            tf_IntegerList_gun_costListNum.forEach(addEventNum -> costListNum.add(addEventNum.getInteger(1)));
            def.gun_costList = StaticUtil.StringListGetter(costList.toArray(),costListNum.toArray());
            def.gun_skillRequirements_Craft = tf_gun_skillRequirements_Craft.getInteger(def.gun_skillRequirements_Craft);
            def.gun_hasStandardCommand = cb_gun_hasStandardCommand.getValue();
            def.gun_warmupTime = tf_gun_warmupTime.getValue(0);
            def.gun_range = tf_gun_range.getInteger(1);
            def.gun_soundCast = tf_gun_soundCast.getValue();
            def.gun_soundCastTail = tf_gun_soundCastTail.getValue();
            def.gun_muzzleFlashScale = tf_gun_muzzleFlashScale.getInteger(def.gun_muzzleFlashScale);
            FileOutputUtil.Output(mainFrame,tf_XAMLName.getValue(), () ->
            {
                try {
                    XamlWriter.GunsWriter(def, chooser.getSelectedFile());
                } catch (IOException ex) {
                    ex.printStackTrace();
                    StaticUtil.errorReport.append(ex.getMessage());
                }
            });
        });

        mainPanel.add(funnyJokes);

        mainFrame.setSize(1200, 500);
        mainFrame.add(scrollPane_2, BorderLayout.CENTER);
        mainFrame.add(errPanel, BorderLayout.EAST);
        mainFrame.setVisible(true);
    }
}
